<template>
  <div class="choosePreferential">
    <div class="title">
      <div class="back-wrapper">
        <back></back>
      </div>
      <div class="clock-wrapper">
        <clock :lockSeatTime="lockSeatTime" :currentTime="currentTime" @hiddren = _toRedirct()></clock>
      </div>
      选择优惠
    </div>
    <loading v-show="isLoading"></loading>
    <div class="content">
      <div class="mask-layer" :class="{isShow: isShow}">
        <div class="title">选择优惠 <span class="cancle" @click="_msgReturnHistory"></span></div>
        <div class="content" ref="mask-layer">
          <div class="mask-layer-wrapper">
            <div class="act-wrapper card-content">
              <div class="card-title">选择优惠券</div>
              <div class="card-content">
                <div v-for="item in seatDiscountCouponList" class="line-block-item" :class="{checked:item.isChecked,unCheck:_unCheckedSeatDiscountCoupon(item.checkedSeatId)}" @click="_checkedCoupon(item)">
                  <div class="left">
                    <div class="name">{{item.name}}</div>
                    <div class="desc" v-show="item.deductionFacePrice">-￥{{item.deductionFacePrice}}</div>
                  </div>
                  <div class="right">
                    <span class="radio"><span class="icon"></span></span>
                  </div>
                </div>
                <div v-for="item in orderDiscountCouponList" class="line-block-item" :class="{checked:item.isChecked,unCheck:_unCheckedOrderDiscountCoupon(item.checkedSeatId)}" @click="_checkedCoupon(item)">
                  <div class="left">
                    <div class="name">{{item.name}}</div>
                    <div class="desc" v-show="item.deductionFacePrice">-￥{{item.deductionFacePrice}}</div>
                  </div>
                  <div class="right">
                    <span class="radio"><span class="icon"></span></span>
                  </div>
                </div>
                <div v-for="item in coupon2DList" class="line-block-item" :class="{checked:item.isChecked,unCheck:_unCheckedExchangeCoupon(item.checkedSeatId)}" @click="_checkedCoupon(item)">
                  <div class="left">
                    <div class="name">{{item.name}}</div>
                    <div class="desc" v-show="item.deductionFacePrice">-￥{{item.deductionFacePrice}}</div>
                  </div>
                  <div class="right">
                    <span class="radio"><span class="icon"></span></span>
                  </div>
                </div>
                <div v-for="item in coupon3DList" class="line-block-item" :class="{checked:item.isChecked,unCheck:_unCheckedExchangeCoupon(item.checkedSeatId)}" @click="_checkedCoupon(item)">
                  <div class="left">
                    <div class="name">{{item.name}}</div>
                    <div class="desc" v-show="item.deductionFacePrice">-￥{{item.deductionFacePrice}}</div>
                  </div>
                  <div class="right">
                    <span class="radio"><span class="icon"></span></span>
                  </div>
                </div>
              </div>
            </div>
            <div class="discount-wrapper card-content">
              <div class="card-title">选择活动</div>
              <div class="card-content">
                <div class="line-block-item" v-for="item in actLabelList" :class="{checked:item.isChecked,unCheck:currentCheckedCouponStatus!='default'}" @click="_checkedActivity(item)">
                  <div class="left">
                    <div class="name">{{item.actName}}</div>
                    <div class="desc" v-show="item.realPayAmount">-￥{{item.realPayAmount}}</div>
                  </div>
                  <div class="right">
                    <span class="radio">
                      <span class="icon"></span>
                    </span>
                  </div>
                </div>
              </div>
            </div>
            <div class="no-choose-wrapper card-content" style="padding-bottom: 30px">
              <div class="card-content">
                <div class="line-block-item " :class="{checked:uncheckedAllActivity}" style="padding: 0 10px;" @click="_uncheckedAllActivity">
                  <div class="left " style="line-height: 44px;">
                    不参加任何活动
                  </div>
                  <div class="right">
                    <span class="radio">
                      <span class="icon"></span>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="btn-wrapper">
          <button class="btn" @click="_msgReturnHistory">确定</button>
        </div>
      </div>
      <div class="items-wrapper">
        <div class="line-block-item" v-for="item in seatCouponList" @click="_showSeatCouponList(item)">
          <div class="left">{{item.seatName}}</div>
          <div class="right">{{_seatChooseCouponText(item)}}</div>
        </div>
      </div>
      <div class="btn-wrapper">
        <button class="btn" @click="_smtPreferentialList">确定</button>
      </div>
    </div>
  </div>
</template>

<script>
  import back from '../../../components/back/back'
  import {getStorage, setStorage} from "../../../common/js/localStorage";
  import {Toast} from 'mint-ui'
  import BScroll from 'better-scroll'
  import clock from '../../../components/clock/clock'
  import loading from '../../../components/loading/loading'
  import global from '../../../common/js/global'
  let scroll = ''
  export default {
    name: "choosePreferential",
    components: {
      'back': back,
      'clock':clock,
      'loading':loading
    },
    data() {
      return {
        isLoading:true,
        lockSeatTime:'2019-02-15 14:37:45',                      //锁座时间
        currentTime:'2019-02-15 14:22:45',                       //当前时间
        seatCouponList:[],                    //提交座位选中卡券参数列表
        seatList: [
          {
            areaServiceFee:0,
            channelRuleFee:4,
            channelRuleFeeId:1,
            colNum:'04',
            rowNum:'1',
            seatId:141374,
            seatName:'1排04座',
            seatNo:'0000000000000001-1-04',
            sectionId:'0000000000000001',
            settlePrice:'35',
            ticketFee:0
          },
          {
            areaServiceFee:0,
            channelRuleFee:4,
            channelRuleFeeId:1,
            colNum:'04',
            rowNum:'1',
            seatId:141375,
            seatName:'1排05座',
            seatNo:'0000000000000001-1-04',
            sectionId:'0000000000000001',
            settlePrice:'35',
            ticketFee:0
          },
          {
            areaServiceFee:0,
            channelRuleFee:4,
            channelRuleFeeId:1,
            colNum:'04',
            rowNum:'1',
            seatId:141376,
            seatName:'1排06座',
            seatNo:'0000000000000001-1-04',
            sectionId:'0000000000000001',
            settlePrice:'35',
            ticketFee:0
          },
          {
            areaServiceFee:0,
            channelRuleFee:4,
            channelRuleFeeId:1,
            colNum:'04',
            rowNum:'1',
            seatId:141377,
            seatName:'1排07座',
            seatNo:'0000000000000001-1-04',
            sectionId:'0000000000000001',
            settlePrice:'35',
            ticketFee:0
          }
        ],                      //座位列表
        couponList:[
          {
            type:1001,
            deductionType:1,
            name:'测试模拟立减01',
            id:10001
          },
          {
            type:1001,
            deductionType:1,
            name:'测试模拟立减02',
            id:10002
          },
          {
            type:1001,
            deductionType:1,
            name:'测试模拟立减03',
            id:10003
          },
          {
            type:1001,
            deductionType:1,
            name:'测试模拟立减04',
            id:10004
          },
          {
            type:1001,
            deductionType:2,
            name:'测试模拟满减01',
            id:20001
          },
          {
            type:1001,
            deductionType:2,
            name:'测试模拟满减02',
            id:20002
          },
          {
            type:1001,
            deductionType:2,
            name:'测试模拟满减03',
            id:20003
          },
          {
            type:1001,
            deductionType:2,
            name:'测试模拟满减04',
            id:20004
          },
          {
            type:1002,
            exchangeType:1,
            name:'2D01',
            id:30001
          },
          {
            type:1002,
            exchangeType:1,
            name:'2D02',
            id:30002
          },
          {
            type:1002,
            exchangeType:1,
            name:'2D03',
            id:30003
          },
          {
            type:1002,
            exchangeType:1,
            name:'2D04',
            id:30004
          },
          {
            type:1002,
            exchangeType:1,
            name:'2D05',
            id:30005
          },
          {
            type:1002,
            exchangeType:2,
            name:'3D01',
            id:40001
          },
          {
            type:1002,
            exchangeType:2,
            name:'3D02',
            id:40002
          },
          {
            type:1002,
            exchangeType:2,
            name:'3D03',
            id:40003
          },
          {
            type:1002,
            exchangeType:2,
            name:'3D04',
            id:40004
          },
          {
            type:1002,
            exchangeType:2,
            name:'3D05',
            id:40005
          },
        ],                     //卡券列表
        actLabelList:[
          {
            actName:'测试活动',
            realPayAmount:'40',
            actId:10001,
            isChecked:false
        },{
            actName:'测试活动2',
            realPayAmount:'40',
            actId:10002,
            isChecked:false
        },{
            actName:'测试活动2',
            realPayAmount:'40',
            actId:10003,
            isChecked:false
        }
        ],                   //活动列表
        checkedActivity:false,                //是否选择活动
        currentCheckedActivitySeat:'',        //当前选择活动座位
        currentCheckedActivityId:'',          //当前选择活动ID
        isShow: false,                        //是否显示浮层
        checkedSeatCouponList:[],             //座位已选卡券列表
        seatDiscountCouponList:[],            //立减券列表
        orderDiscountCouponList:[],           //满减券列表
        coupon2DList:[],                      //2D券列表
        coupon3DList:[],                      //3D券列表
        uncheckedAllActivity:true,            //不选择任何活动
        currentCheckedSeatId:'',              //当前选中座位Id
        currentCheckedCouponStatus:'default', //当前选择卡券状态
        excCouponConditionList:[
          {
            exchange2dCount:2,
            exchange3dCount:3,
          },{
            exchange2dCount:1,
            exchange3dCount:4,
          },{
            exchange2dCount:0,
            exchange3dCount:4,
          },{
            exchange2dCount:3,
            exchange3dCount:2,
          },{
            exchange2dCount:4,
            exchange3dCount:1,
          },{
            exchange2dCount:0,
            exchange3dCount:4,
          },{
            exchange2dCount:2,
            exchange3dCount:1,
          },{
            exchange2dCount:4,
            exchange3dCount:2,
          },{
            exchange2dCount:4,
            exchange3dCount:0,
          },

        ],         //2D券3D券转换规则
        currentCheckedSeat2DNum:0,            //当前座位2D券数量
        currentCheckedSeat3DNum:0,            //当前座位3D券数量
        currentCheckedSeat2DMaxNum:0,         //当前座位2D券最大限额
        currentCheckedSeat3DMaxNum:0,         //当前座位3D券最大限额
      }
    },
    mounted() {
      console.log(getStorage('chooseSeatInfo'))
      this.lockSeatTime = getStorage('chooseSeatInfo').lockSeatTime
      this.currentTime = getStorage('chooseSeatInfo').currentTime
      this._getChecked2D3DCouponMaxNum()
      this._getChoosePreferentialListInfo()
      //卡券分类
      this._classificationCoupon()
      // this._regroupSeatList()
    },
    methods: {
      //返回选座
      _toRedirct(bool){
        if(bool){
          window.history.go(-1)
        }
      },
      //数据回档
      _msgReturnHistory(){
        this.isShow = false
      },
      //提交优惠列表
      _smtPreferentialList(){
        //遍历筛选数据
        let seatCouponList = this.seatCouponList
        for(let i=0;i<seatCouponList.length;i++){
          let couponIdList = []
          let seatId = seatCouponList[i].seatId
          let obj = {}
          for(let j=0;j<seatCouponList[i].couponList.length;j++){
            couponIdList.push(seatCouponList[i].couponList[j].id)
          }
          obj = {
            seatId:seatId,
            couponIdList:couponIdList
          }
          seatCouponList[i] = obj
        }
        this.$axios(
          {
            method:'post',
            headers:{
              "Content-Type":"application/json",
              "MOBILE-DEVICE":'PSD_H5_1_MPS',
              "JSESSIONID":getStorage('userInfo').JSESSIONID
            },
            url:global.$host+'/member/preBuy',
            data:{
              lockSeatFlowId:getStorage('chooseSeatInfo').lockSeatFlowId,
              seatCouponList:seatCouponList
            }
          }
        ).then((res)=>{
          if(res.status===global.ERR_OK){
            if(res.data.code===0){
              let data = res.data.data
              Toast('success!!!!!')
            }else {
              Toast(res.data.errorMsg)
            }
          }else {
            Toast('服务器错误，错误代码:'+res.status)
          }

        })

      },
      //获取优惠列表信息
      _getChoosePreferentialListInfo(){
        this.isLoading = true
        this.$axios(
          {
            method:'post',
            headers:{
              "Content-Type":"application/json",
              "MOBILE-DEVICE":'PSD_H5_1_MPS',
              "JSESSIONID":getStorage('userInfo').JSESSIONID
            },
            url:global.$host+'/member/discountList',
            data:{
              lockSeatFlowId:getStorage('chooseSeatInfo').lockSeatFlowId,
            }
          }
        ).then((res)=>{
          if(res.status===global.ERR_OK){
            if(res.data.code===0){
              this.isLoading = false
              let data = res.data.data
              this.seatList = data.seatList
              this.actLabelList = data.actLabelList
              this.couponList = data.couponList
              this.excCouponConditionList = data.excCouponConditionList

              this.$nextTick(() => {
                if (!this.scroll) {
                  scroll = new BScroll(this.$refs['mask-layer'], {click: true,startX:0})
                } else {
                  this.scroll.refresh();
                }
              })

            }else {
              Toast(res.data.errorMsg)
            }
          }else {
            Toast('服务器错误，错误代码:'+res.data.status)
          }

        })
      },
      //返回显示文本
      _seatChooseCouponText(item){
        let noChooseText = '请选择优惠活动/优惠券'
        let chooseCouponText = ''
        let chooseActivityText = ''
        if(this.checkedActivity){
          return '已选择1条活动'
        }
        for(let i=0;i<this.seatCouponList.length;i++){
          if(item.seatId===this.seatCouponList[i].seatId){
            if(this.seatCouponList[i].couponList.length){
              return '已选择'+this.seatCouponList[i].couponList.length+'张卡券'
            }else{
              return noChooseText
            }
          }
        }
        return noChooseText
      },
      //选择活动
      _checkedActivity(act){
        console.log(act)
        if(this.currentCheckedCouponStatus!='default'){
          return
        }
        let actLabelList = this.actLabelList
        for(let i=0;i<actLabelList.length;i++){
          if(act.id===actLabelList[i].id){
            actLabelList[i].isChecked=!actLabelList[i].isChecked
            this.currentCheckedActivitySeat = this.currentCheckedSeatId
            this.currentCheckedActivityId = act.actId
            this.checkedActivity = true
            if(!actLabelList[i].isChecked){
              this.currentCheckedActivitySeat = ''
              this.currentCheckedActivityId = ''
              this.checkedActivity = false
            }
          }else{
            actLabelList[i].isChecked=false
          }
        }
        console.log(this.currentCheckedActivitySeat)
        console.log(this.currentCheckedActivityId)
        this.actLabelList = []
        this.actLabelList = actLabelList
        console.log(this.checkedActivity)
      },
      //不参加任何活动
      _uncheckedAllActivity(){
        if(this.uncheckedAllActivity){
          this.uncheckedAllActivity = false
        }else {
          this.uncheckedAllActivity = true
        }
      },
      //重置数组用于双向数据绑定
      _regroupAllCouponList(){
        let seatDiscountCouponList = this.seatDiscountCouponList
        this.seatDiscountCouponList = []
        this.seatDiscountCouponList = seatDiscountCouponList
        let orderDiscountCouponList = this.orderDiscountCouponList
        this.orderDiscountCouponList = []
        this.orderDiscountCouponList = orderDiscountCouponList
        let coupon2DList = this.coupon2DList
        this.coupon2DList = []
        this.coupon2DList = coupon2DList
        let coupon3DList = this.coupon3DList
        this.coupon3DList = []
        this.coupon3DList = coupon3DList
      },
      //判断兑换券能否被选中
      _unCheckedExchangeCoupon(id){
        if(this.checkedActivity){
          return true
        }
        if(this.currentCheckedCouponStatus==='default'){
          return false
        }
        if(this.currentCheckedCouponStatus!='exchangeCouponStatus'){
          return true
        }
        if(id===''){
          return false
        }else {
          if(this.currentCheckedSeatId!=id){
            return true
          }
        }
      },
      //判断立减券能否被选中
      _unCheckedSeatDiscountCoupon(id){
        if(this.checkedActivity){
          return true
        }
        if(this.currentCheckedCouponStatus==='default'){
          return false
        }
        if(this.currentCheckedCouponStatus!='seatCouponCheckedStatus'){
          return true
        }
        if(id===''){
          return false
        }else{
          if(this.currentCheckedSeatId!=id){
            return true
          }
        }
      },
      //判断满减券当前能否被选中
      _unCheckedOrderDiscountCoupon(id){
        if(this.checkedActivity){
          return true
        }
        if(this.currentCheckedCouponStatus==='default'){
          return false
        }
        if(this.currentCheckedCouponStatus!='orderCouponCheckedStatus'){
          return true
        }
        if(id===''){
          return false
        }else{
          if(this.currentCheckedSeatId!=id){
            return true
          }
        }
      },
      //初始化提交数据列表
      _regroupSeatList(){
        this.seatCouponList = []
        for(let i=0;i<this.seatList.length;i++){
          let seat = {
            seatId:this.seatList[i].seatId,
            seatName:this.seatList[i].seatName,
            couponList:[]
          }
          this.seatCouponList.push(seat)
        }
      },
      //选择卡券
      _checkedCoupon(item){
        let couponId = item.id
        let couponType = this._couponType(item)
        let currentCheckedCouponStatus = this.currentCheckedCouponStatus
        let orderDiscountCouponList = this.orderDiscountCouponList
        let seatDiscountCouponList = this.seatDiscountCouponList
        let coupon2DList = this.coupon2DList
        let coupon3DList = this.coupon3DList
        let seatCouponList = this.seatCouponList
        if(this.checkedActivity){
          return true
        }
        switch (couponType) {
          case 'seatDiscountCoupon':
            if(currentCheckedCouponStatus==='default'){
              this.currentCheckedCouponStatus = 'seatCouponCheckedStatus'
              console.log(this.currentCheckedCouponStatus)
              for(let i=0;i<seatDiscountCouponList.length;i++){
                if(seatDiscountCouponList[i].id===couponId){
                  seatDiscountCouponList[i].isChecked = true
                  seatDiscountCouponList[i].checkedSeatId = this.currentCheckedSeatId
                }else {
                  seatDiscountCouponList[i].checkedSeatId = ''
                }
              }
              for(let i=0;i<seatCouponList.length;i++){
                if(seatCouponList[i].seatId===this.currentCheckedSeatId){
                  seatCouponList[i].couponList = [item]
                }else {
                  seatCouponList[i].couponList = []
                }
              }
            }else if(currentCheckedCouponStatus === 'seatCouponCheckedStatus'){
              if(item.checkedSeatId!=this.currentCheckedSeatId&&item.checkedSeatId!=''){
                return
              }else{
                //判断卡券是否被选中是否是当前座位
                if(item.isChecked){
                  let isDefault = 0
                  if(item.checkedSeatId === this.currentCheckedSeatId){
                    for(let i=0;i<seatDiscountCouponList.length;i++){
                      if(seatDiscountCouponList[i].checkedSeatId===this.currentCheckedSeatId){
                        seatDiscountCouponList[i].isChecked = false
                        seatDiscountCouponList[i].checkedSeatId = ''
                      }
                      if(!seatDiscountCouponList[i].isChecked){
                        isDefault++
                      }
                    }
                    if(isDefault===seatDiscountCouponList.length){
                      this.currentCheckedCouponStatus = 'default'
                    }
                    for(let i = 0;i<seatCouponList.length;i++){
                      if(seatCouponList[i].seatId === this.currentCheckedSeatId){
                        seatCouponList[i].couponList = []
                      }
                    }
                  }
                }else{
                  for(let i=0;i<seatCouponList.length;i++){
                    if(this.currentCheckedSeatId===seatCouponList[i].seatId){
                      seatCouponList[i].couponList = [item]
                    }
                  }
                  for(let i=0;i<seatDiscountCouponList.length;i++){
                    if(couponId===seatDiscountCouponList[i].id){
                      seatDiscountCouponList[i].isChecked = true
                      seatDiscountCouponList[i].checkedSeatId = this.currentCheckedSeatId
                    }else{
                      if(seatDiscountCouponList[i].checkedSeatId === this.currentCheckedSeatId){
                        seatDiscountCouponList[i].isChecked = false
                        seatDiscountCouponList[i].checkedSeatId = ''
                      }
                    }
                  }
                }
              }
            }
            this.seatCouponList = []
            this.seatCouponList = seatCouponList
            this.seatDiscountCouponList = []
            this.seatDiscountCouponList = seatDiscountCouponList
            break
          case 'orderDiscountCoupon':
            if(currentCheckedCouponStatus==='default'){
              //若当前状态为default则选中当前卡券并在当前选中座位id下插入该卡券，并为该卡券设置座位flag以便下次勾选
              this.currentCheckedCouponStatus = 'orderCouponCheckedStatus'
              for(let i=0;i<orderDiscountCouponList.length;i++){
                if(orderDiscountCouponList[i].id === couponId){
                  orderDiscountCouponList[i].isChecked = true
                  this.orderDiscountCouponList = []
                  this.orderDiscountCouponList = orderDiscountCouponList
                  for(let j=0;j<seatCouponList.length;j++){
                    if(seatCouponList[j].seatId===this.currentCheckedSeatId){
                      seatCouponList[j].couponList = [item]
                    }
                  }
                }
                orderDiscountCouponList[i].checkedSeatId = this.currentCheckedSeatId
              }
              this.seatCouponList = []
              this.seatCouponList = seatCouponList
            }else if(currentCheckedCouponStatus==='orderCouponCheckedStatus'){
              if(item.checkedSeatId!=this.currentCheckedSeatId){
                return
              }else{
                if(item.isChecked){
                  for(let i=0;i<orderDiscountCouponList.length;i++){
                    if(orderDiscountCouponList[i].id===couponId){
                      orderDiscountCouponList[i].isChecked = false
                    }
                    orderDiscountCouponList[i].checkedSeatId = ''
                  }
                  for(let i=0;i<seatCouponList.length;i++){
                    seatCouponList[i].couponList = []
                  }
                  this.currentCheckedCouponStatus = 'default'
                  this.orderDiscountCouponList = []
                  this.orderDiscountCouponList = orderDiscountCouponList
                }else{
                  for(let i=0;i<orderDiscountCouponList.length;i++){
                    if(orderDiscountCouponList[i].id===couponId){
                      orderDiscountCouponList[i].isChecked = true
                    }else {
                      orderDiscountCouponList[i].isChecked = false
                    }
                  }
                  for(let i=0;i<seatCouponList.length;i++){
                    if(seatCouponList[i].seatId === this.currentCheckedSeatId){
                      seatCouponList[i].couponList = [item]
                    }else {
                      seatCouponList[i].couponList = []
                    }
                  }
                  this.orderDiscountCouponList = []
                  this.orderDiscountCouponList = orderDiscountCouponList
                }
                this.seatCouponList = seatCouponList
                console.log(this.seatCouponList)
              }
            }
            break
          case 'coupon2D':
          case 'coupon3D':
            // console.log(couponType)
            if(currentCheckedCouponStatus==='default'){
              this.currentCheckedCouponStatus='exchangeCouponStatus'
              if(couponType==='coupon2D'){
                if(1>this.currentCheckedSeat2DMaxNum){
                  //判断当前座位选择2D3D券最大数量是否大于当前所有添加券数量
                  this.currentCheckedCouponStatus='default'
                  return
                }
                for(let i=0;i<coupon2DList.length;i++){
                  if(couponId===coupon2DList[i].id){
                    coupon2DList[i].isChecked = true
                    coupon2DList[i].checkedSeatId = this.currentCheckedSeatId
                  }
                }
              }else{
                if(1>this.currentCheckedSeat3DMaxNum){
                  //判断当前座位选择2D3D券最大数量是否大于当前所有添加券数量
                  this.currentCheckedCouponStatus='default'
                  return
                }
                for(let i=0;i<coupon3DList.length;i++){
                  if(couponId===coupon3DList[i].id){
                    coupon3DList[i].isChecked = true
                    coupon3DList[i].checkedSeatId = this.currentCheckedSeatId
                  }
                }
              }
              for(let i=0;i<seatCouponList.length;i++){
                if(this.currentCheckedSeatId===seatCouponList[i].seatId){
                  seatCouponList[i].couponList = [item]
                }else {
                  seatCouponList[i].couponList = []
                }
              }
            }else if(currentCheckedCouponStatus==='exchangeCouponStatus'){
              //判断当前卡券是否被选中，是否在本次选中座位下
              if(item.isChecked){
                //如果被选中并在当前座位下则移除当前卡券
                if(item.checkedSeatId!=this.currentCheckedSeatId){
                  return
                }
                if(couponType==='coupon2D'){
                  for(let i=0;i<coupon2DList.length;i++){
                    if(coupon2DList[i].id===couponId){
                      console.log(couponId)
                      coupon2DList[i].isChecked = false
                      coupon2DList[i].checkedSeatId = ''
                    }
                  }
                  for(let i=0;i<seatCouponList.length;i++){
                    if(seatCouponList[i].seatId === this.currentCheckedSeatId){
                      for(let j=0;j<seatCouponList[i].couponList.length;j++){
                        if(seatCouponList[i].couponList[j].id===couponId){
                          seatCouponList[i].couponList.splice(j,1)
                          break
                        }
                      }
                    }
                  }
                }
                if(couponType==='coupon3D'){
                  for(let i=0;i<coupon3DList.length;i++){
                    if(coupon3DList[i].id===couponId){
                      coupon3DList[i].isChecked = false
                      coupon3DList[i].checkedSeatId = ''
                    }
                  }
                  for(let i=0;i<seatCouponList.length;i++){
                    if(seatCouponList[i].seatId === this.currentCheckedSeatId){
                      for(let j=0;j<seatCouponList[i].couponList.length;j++){
                        if(seatCouponList[i].couponList[j].id===couponId){
                          seatCouponList[i].couponList.splice(j,1)
                          break
                        }
                      }
                    }
                  }
                }
                //判断并赋值当前currentCheckedCouponStatus,重置数组中的checkedSeatId
                let defaultMask = 0
                for(let i=0;i<coupon2DList.length;i++){
                  if(coupon2DList[i].isChecked){
                    defaultMask++
                  }
                }
                for(let i=0;i<coupon3DList.length;i++){
                  if(coupon3DList[i].isChecked){
                    defaultMask++
                  }
                }
                if(defaultMask===0){
                  this.currentCheckedCouponStatus = 'default'
                }
              }else{
                if(couponType==='coupon2D'){
                  //获取当前座位下2D券数目
                  this._getCurrentCheckedSeat2D3DCouponNum()
                  if(this.currentCheckedSeat2DNum===this.currentCheckedSeat2DMaxNum){
                    Toast('当前座位最多选择'+this.currentCheckedSeat2DMaxNum+'张2D券')
                    return
                  }
                  if(!this._excCouponRule(couponType)){
                    Toast('所选卡券已符合规则')
                    return
                  }
                  for(let i=0;i<coupon2DList.length;i++){
                    if(couponId===coupon2DList[i].id){
                      coupon2DList[i].isChecked = true
                      coupon2DList[i].checkedSeatId = this.currentCheckedSeatId
                    }
                  }

                }else{
                  this._getCurrentCheckedSeat2D3DCouponNum()
                  console.log(this.currentCheckedSeat3DNum)
                  if(this.currentCheckedSeat3DNum===this.currentCheckedSeat3DMaxNum){
                    Toast('当前座位最多选择'+this.currentCheckedSeat3DMaxNum+'张3D券')
                    return
                  }
                  if(!this._excCouponRule(couponType)){
                    Toast('所选卡券已符合规则')
                    return
                  }
                  for(let i=0;i<coupon3DList.length;i++){
                    if(couponId===coupon3DList[i].id){
                      coupon3DList[i].isChecked = true
                      coupon3DList[i].checkedSeatId = this.currentCheckedSeatId
                    }
                  }
                }
                for(let i=0;i<seatCouponList.length;i++){
                  if(this.currentCheckedSeatId===seatCouponList[i].seatId){
                    seatCouponList[i].couponList.push(item)
                  }
                }
              }
            }
            this.coupon2DList = []
            this.coupon2DList = coupon2DList
            this.seatCouponList = []
            this.seatCouponList = seatCouponList
            break
        }
      },
      //卡券分类
      _classificationCoupon(){
        this.orderDiscountCouponList = []
        this.seatDiscountCouponList = []
        this.coupon2DList = []
        this.coupon3DList = []
        for(let i=0;i<this.couponList.length;i++){
          let couponType = this._couponType(this.couponList[i])
          this.couponList[i].isChecked = false
          this.couponList[i].checkedSeatId = ''
          switch (couponType) {
            case 'seatDiscountCoupon':
              this.seatDiscountCouponList.push(this.couponList[i])
                  break
            case 'orderDiscountCoupon':
              this.orderDiscountCouponList.push(this.couponList[i])
                  break
            case 'coupon2D':
              this.coupon2DList.push(this.couponList[i])
                  break
            case 'coupon3D':
              this.coupon3DList.push(this.couponList[i])
                  break
          }
        }
      },
      //显示卡券列表保存当前信息
      _showSeatCouponList(item){
        //获取当前所有选座信息
        this.isShow = true
        this.currentCheckedSeatId = 0
        this.currentCheckedSeatId = item.seatId
        this._getCurrentCheckedSeat2D3DCouponNum()
        // console.log(this.scroll)
        scroll.scrollTo(0,0,0)
      },
      //返回当前卡券类型
      _couponType(item){
        let couponType = ''
        //判断是否为优惠券 type: 1001 优惠券 1002 兑换券
        if(item.type===1001){
          item.deductionType ===1?couponType='seatDiscountCoupon':couponType='orderDiscountCoupon'
        }else {
          item.exchangeType ===1?couponType='coupon2D':couponType='coupon3D'
        }
        return couponType
      },
      //获取当前选中座位中2D3D券数量
      _getCurrentCheckedSeat2D3DCouponNum(){
        let currentCheckedSeat2DNum = 0
        let currentCheckedSeat3DNum = 0
        //动态获取当前选中座位2D券数量3D券数量
        for(let i=0;i<this.seatCouponList.length;i++){
          if(this.seatCouponList[i].seatId===this.currentCheckedSeatId){
            // console.log(this.seatCouponList[i])
            for(let j=0;j<this.seatCouponList[i].couponList.length;j++){
              if(this._couponType(this.seatCouponList[i].couponList[j])==='coupon2D'){
                currentCheckedSeat2DNum++
              }
              if(this._couponType(this.seatCouponList[i].couponList[j])==='coupon3D'){
                currentCheckedSeat3DNum++
              }
            }
          }
        }
        this.currentCheckedSeat2DNum = currentCheckedSeat2DNum
        this.currentCheckedSeat3DNum = currentCheckedSeat3DNum
      },
      //获取2D3D券最大选择数量
      _getChecked2D3DCouponMaxNum(){
        let currentCheckedSeat2DMaxNum = 0
        let currentCheckedSeat3DMaxNum = 0
        let excCouponConditionList = this.excCouponConditionList
        for(let i=0;i<excCouponConditionList.length;i++){
          if(excCouponConditionList[i].exchange2dCount>currentCheckedSeat2DMaxNum){
            currentCheckedSeat2DMaxNum = excCouponConditionList[i].exchange2dCount
          }
          if(excCouponConditionList[i].exchange3dCount>currentCheckedSeat3DMaxNum){
            currentCheckedSeat3DMaxNum = excCouponConditionList[i].exchange3dCount
          }
        }
        this.currentCheckedSeat2DMaxNum = currentCheckedSeat2DMaxNum
        this.currentCheckedSeat3DMaxNum = currentCheckedSeat3DMaxNum
      },
      //判断当前选中卡券是否符合规则
      _excCouponRule(couponType){
        let coupon3DMax = 0
        let coupon2DMax = 0
        let currentCheckedSeat2DNum = this.currentCheckedSeat2DNum
        let currentCheckedSeat3DNum = this.currentCheckedSeat3DNum
        let excCouponConditionList = this.excCouponConditionList
        if(couponType==='coupon2D'){
          //获取3D券数量进行规则匹配判断当前2D券是否可以点击
          let couponNumList = []
          for(let i=0;i<excCouponConditionList.length;i++){
            if(excCouponConditionList[i].exchange3dCount===currentCheckedSeat3DNum){
              couponNumList.push(excCouponConditionList[i])
            }
          }
          //获取当前3D券数量2D券可选最大值
          for(let i=0;i<couponNumList.length;i++){
            if(couponNumList[i].exchange2dCount>coupon2DMax){
              coupon2DMax = couponNumList[i].exchange2dCount
            }
          }
          if(this.currentCheckedSeat2DNum<coupon2DMax){
            return true
          }else {
            return false
          }
        }else{
         //获取2D券数量进行规则匹配判断当前3D券是否可以点击
          let couponNumList = []
          for(let i=0;i<excCouponConditionList.length;i++){
            if(excCouponConditionList[i].exchange2dCount===currentCheckedSeat2DNum){
              couponNumList.push(excCouponConditionList[i])
            }
          }
          //获取当前2D券数量3D券可选最大值
          for(let i=0;i<couponNumList.length;i++){
            if(couponNumList[i].exchange3dCount>coupon3DMax){
              coupon3DMax = couponNumList[i].exchange3dCount
            }
          }
          console.log(couponNumList)
          if(this.currentCheckedSeat3DNum<coupon3DMax){
            return true
          }else {
            return false
          }
        }
      }
    },
    watch: {
      'couponList'(){
        this._classificationCoupon()
      },
      'seatList'(){
        this._regroupSeatList()
      },
      'checkedActivity'(){
        if(this.checkedActivity){
          if(this.currentCheckedCouponStatus==='default'){
            this.uncheckedAllActivity=false
          }
        }
      },
      'uncheckedAllActivity'(){
       if(this.uncheckedAllActivity){
         this._classificationCoupon()
         this._regroupSeatList()
         this.currentCheckedCouponStatus='default'
         this.checkedActivity = false
         let actLabelList  = this.actLabelList
         for(let i= 0;i<actLabelList.length;i++){
           actLabelList[i].isChecked = false
         }
         this.actLabelList = []
         this.actLabelList = actLabelList
       }
      },
      'currentCheckedCouponStatus'(){
        this._regroupAllCouponList()
        if(this.currentCheckedCouponStatus==='default'){
          this.uncheckedAllActivity = true
        }else {
          this.uncheckedAllActivity = false
          console.log(this.currentCheckedCouponStatus)
        }
      },
      'currentCheckedSeatId'(){
        let currentCheckedSeat2DNum = 0
        let currentCheckedSeat3DNum = 0
        //动态获取当前选中座位2D券数量3D券数量
        for(let i=0;i<this.seatCouponList.length;i++){
          if(this.seatCouponList[i].seatId===this.currentCheckedSeatId){
            console.log(this.seatCouponList[i])
            for(let j=0;j<this.seatCouponList[i].couponList.length;j++){
              if(this._couponType(this.seatCouponList[i].couponList[j])==='coupon2D'){
                currentCheckedSeat2DNum++
              }
              if(this._couponType(this.seatCouponList[i].couponList[j])==='coupon3D'){
                currentCheckedSeat3DNum++
              }
            }
          }
        }
      }
    }
  }
</script>

<style scoped lang="stylus">
  .choosePreferential
    position absolute
    height 100%
    width 100%
    background #101011
    user-select none
    overflow hidden
    .title
      position relative
      padding 10px 0
      height 22px
      line-height 22px
      text-align center
      font-size 16px
      color #ffffff
      .back-wrapper
        position absolute
        left 0
        top 10px
        width 36px
        height 22px
      .clock-wrapper
        position absolute
        right -14px
        top 13px
        width 48px
        padding-left 20px
        text-align left
        border 1px solid #f7c20f
        height 18px
        font-size 10px
        line-height 18px
        color #f7c20f
        border-radius 9px
        background url("icon_clock.svg") no-repeat 3px center /14px 14px
    .content
      height calc(100% - 52px)
      overflow hidden
      padding 10px 0px 0px 0px
      .mask-layer
        position absolute
        top 0
        left 0
        z-index 1
        background #101011
        width 100%
        height 100%
        transform translate3d(0, 100%, 0)
        transition transform .5s
        &.isShow
          transform translate3d(0, 0, 0)
          transition transform .5s
        .title
          position relative
          padding 10px 0
          height 22px
          line-height 22px
          text-align center
          font-size 16px
          color #ffffff
          .cancle
            display block
            height 16px
            width 16px
            position absolute
            background url("icon_cancel.svg") no-repeat center / 16px 16px
            right 10px
            top 14px
        .content
          height calc(100% - 88px)
          .card-content
            color #ffffff
            font-size 12px
            text-align left
            .card-title
              color #8e8e8e
              padding 10px
          .card-content
            .line-block-item
              display flex
              height 44px
              background #171717
              margin-bottom 10px
              padding 15px 10px
              &.checked
                .right
                  .radio
                    background #f7c20f
                    transition background 0.3s
                    .icon
                      opacity 1
                      transition opacity 0.3s
              &.unCheck
                .left
                  color #7e8c8d
                .right
                  .radio
                    background #7e8c8d
              .left
                flex 1
                line-height 22px
                font-size 14px
                .name
                  height 22px
                .desc
                  height 22px
                  color #f7c20f
              .right
                position relative
                flex 0 0 90px
                .radio
                  position absolute
                  right 0
                  top 13px
                  display block
                  height 18px
                  width 18px
                  background #3e4147
                  border-radius 9px
                  overflow hidden
                  transition background 0.3s
                  .icon
                    display block
                    height 18px
                    width 18px
                    background url("icon_checked.svg")no-repeat center/13px 13px
                    opacity 0
                    transition opacity 0.3s
      .btn-wrapper
        position absolute
        width 100%
        bottom 0
        .btn
          background #f7c20f
          width 100%
          border none
          height 44px
          font-size 16px
          color #161719
      .items-wrapper
        .line-block-item
          display flex
          font-size 14px
          background #171717
          height 42px
          line-height 42px
          padding 0 10px
          margin-bottom 10px
          .left
            flex 0 0 80px
            color #ffffff
            text-align left
          .right
            flex 1
            text-align right
            color #f5c110
            padding-right 15px
            background url("arrow_right.svg") no-repeat 100% center / 6px 12px
</style>
